<?php

namespace User\Controller;


use Think\Page;
use User\Model\FactoryModel;
use User\Model\RecommendModel;

class RecommendController extends UserController
{
    public function index()
    {
        $cond['status'] = array("NEQ", RecommendModel::STATUS_DELETE);
        $cond['token'] = $this->wxuser['token'];

        $count = M("recommend")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("recommend")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('rec_id desc')->select();
        $this->assign("list", $list);
        return $this->display();
    }

    public function add()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $type = 'add';
            $this->assign('type', $type);
            return $this->display('update');
        }
        if (empty($params['name'])) {
            return $this->my_error("请填写名称");
        }
        $data['name'] = $params['name'];
        $data['token'] = $this->wxuser['token'];
        $data['start_date'] = $params['start_date'];
        $data['end_date'] = $params['end_date'];
        $data['status'] = RecommendModel::STATUS_PENDING;
        $picurl = I("post.picurl", "", "");
        if (!empty($picurl)) {
            $res = FactoryModel::ajaxImgUpload($picurl, "recommend");
            if ($res['code'] == 1) {
                $data['picurl'] = $res['msg'];
            }
        }
        $data['desc'] = $params['desc'];
        $data['url'] = $params['url'];
        $data['ori_id'] = $params['ori_id'];
        $data['wechat_name'] = $params['wechat_name'];
        M("recommend")->add($data);
        return $this->my_success("成功");
    }

    public function update()
    {
        $type = I("get.type", "", "");
        $recId = I("get.recId", "", "");
        $params = I("post.", "", "");
        if ($type == 'update' && empty($params)) {
            $recommend = M("recommend")->where(['rec_id' => $recId])->find();
            $this->assign("recommend", $recommend);
            return $this->display();
        }
        $recId = I("post.recId", "", "");
        if (empty($recId)) {
            return $this->my_error("选择的任务宝信息有误");
        }
        $data['name'] = $params['name'];
        $data['token'] = $this->wxuser['token'];
        $data['start_date'] = $params['start_date'];
        $data['end_date'] = $params['end_date'];
        $data['status'] = RecommendModel::STATUS_PENDING;
        $picurl = I("post.picurl", "", "");
        if (!empty($picurl)) {
            $res = FactoryModel::ajaxImgUpload($picurl, "recommend");
            if ($res['code'] == -1) {
                return $this->my_error($res['msg']);
            }
            $data['picurl'] = $res['msg'];
        }
        $data['desc'] = $params['desc'];
        $data['url'] = $params['url'];
        $data['ori_id'] = $params['ori_id'];
        $data['wechat_name'] = $params['wechat_name'];
        M("recommend")->where(['rec_id' => $recId])->save($data);
        return $this->my_success("成功");
    }

    public function delete()
    {
        $recId = I("get.recId", "", "");
        $data['status'] = RecommendModel::STATUS_DELETE;
        if (!empty($recId)) {
            M("recommend")->where(['rec_id' => $recId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function pass()
    {
        $recId = I("get.recId", "", "");
        $data['status'] = RecommendModel::STATUS_ACTIVE;
        if (!empty($recId)) {
            M("recommend")->where(['rec_id' => $recId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function unpass()
    {
        $recId = I("get.recId", "", "");
        $data['status'] = RecommendModel::STATUS_PENDING;
        if (!empty($recId)) {
            M("recommend")->where(['rec_id' => $recId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function dindex()
    {
        $cond['rec_id'] = I("get.recId", "", "");
        $count = M("wuser_recommend")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("wuser_recommend")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();
        $this->assign("list", $list);
        return $this->display();
    }

    public function prize()
    {
        $cond['rec_id'] = I("get.recId", "", "");
        $rec_prize = M("recommend_prize")->where($cond)->find();
        $this->assign("rec_prize", $rec_prize);
        return $this->display();
    }

    public function pri_update()
    {
        $params = I("post.", "", "");
        $data['name'] = $params['name'];
        $data['rec_id'] = $params['recId'];
        $data['num'] = $params['num'] ? $params['num'] : 0;
        $data['pri_name'] = $params['pri_name'] ? $params['pri_name'] : '';
        $data['pri_content'] = $params['pri_content'] ? $params['pri_content'] : '';
        $data['success_desc'] = $params['success_desc'] ? $params['success_desc'] : '';
        $data['pri_num'] = $params['pri_num'] ? $params['pri_num'] : 0;
        $picurl = I("post.picurl", "", "");
        if (!empty($picurl)) {
            $res = FactoryModel::ajaxImgUpload($picurl, "recommend");
            if ($res['code'] == -1) {
                return $this->my_error($res['msg']);
            }
            $data['pri_pic'] = $res['msg'];
        }
        if (empty($params['priId'])) {
            M("recommend_prize")->add($data);
        } else {
            $data['pri_id'] = $cond['pri_id'] = $params['priId'];
            M("recommend_prize")->where($cond)->save($data);
        }
        return $this->my_success("成功");
    }
}